﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CircleTrigger : MonoBehaviour
{
    /// <summary>
    /// 避免玩家离开球体时出现多次与球体碰撞问题
    /// </summary>
    public bool canCollisionByPlayer = true;
    private Vector2 lastLeavePos;
    private int leaveCount;
    private Coroutine getPlayerPosCoroutine;
    private Player _player;
    private ObjectPool objectPool;
    private AutoGenerateItems autoGenerateItems;
    private AddScoreMoudle addScoreMoudle;

    private Sprite rawSprite;
    private SpriteRenderer m_SpriteRenderer;

    private void Awake()
    {
        _player = GameObject.Find("/Player").GetComponent<Player>();
        objectPool = GameObject.Find("/ObjectPool").GetComponent<ObjectPool>();
        autoGenerateItems = GameObject.Find("/AutoGenerateItems").GetComponent<AutoGenerateItems>();
        gameObject.name = "Circle";
        addScoreMoudle = GameObject.Find("AddScoreMoudle").GetComponent<AddScoreMoudle>();

        m_SpriteRenderer = GetComponent<SpriteRenderer>();
        rawSprite = m_SpriteRenderer.sprite;
        
    }

    private void OnEnable()
    {
        float _randomXY = Random.Range(1.35f, 2.2f);
        transform.localScale = new Vector3(_randomXY, _randomXY, 1.0f);
        transform.SetAsLastSibling();
        m_SpriteRenderer.sprite = rawSprite;
    }

    private void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.gameObject.tag == "Player" && canCollisionByPlayer)
        {
            foreach (var item in FindObjectsOfType<CircleTrigger>())
                item.canCollisionByPlayer = true;

            canCollisionByPlayer = false;
            GetComponent<Animator>().SetTrigger("Trigger");
            _player.CollisionByCircle(transform.Find("Circle_Anim"));
            leaveCount = 0;
            CheckNextCircle();
            addScoreMoudle.AddScoreEvent.Invoke(1, transform.position);
        }
    }

    private void CheckNextCircle()
    {
        Transform _object = transform.parent.GetChild(transform.parent.childCount - 1);
        if (Vector2.Distance(_object.position, _player.transform.position) < 8.0f)
        {
            float random_y = Random.Range(-2.5f, 2.5f);
            if(_object.position.y + random_y > .0f)
            {
                //Debug.Log("1");
                autoGenerateItems.Invoke(_object.position + new Vector3(Random.Range(2.5f, 3.0f), random_y, .0f));
            }
            else
            { 
                //Debug.Log("2");
                autoGenerateItems.Invoke(_object.position + new Vector3(Random.Range(2.5f, 3.0f), Random.Range(2.5f, 3.0f), .0f));
            }
        }
    }

    private void OnCollisionExit2D(Collision2D collision)
    {
        if(collision.gameObject.tag == "Player")
        {
            leaveCount++;
            lastLeavePos = collision.transform.position;

            if(leaveCount >= 2)
            {
                if(getPlayerPosCoroutine != null)
                {
                    StopCoroutine(getPlayerPosCoroutine);
                    getPlayerPosCoroutine = null;
                }
                

                StartCoroutine(GetPlayerPos(collision.transform));
            }
        }
    }

    private IEnumerator GetPlayerPos(Transform player)
    {
        while(Vector2.Distance(lastLeavePos, player.position) < 0.65f)
        {
            yield return 0;
        }
        canCollisionByPlayer = true;
    }


    private void CheckWithPlayerDistance()
    {
        if (Vector2.Distance(transform.position, _player.transform.position) > 16.5f)
        {
            objectPool.Push(gameObject);
            enabled = false;
        }
    }

    private void Update()
    {
        CheckWithPlayerDistance();
    }
}
